package com.jieshuibao.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jieshuibao.common.po.PageRes;
import com.jieshuibao.model.db.Tvbox;
import com.jieshuibao.model.db.Video;
import com.jieshuibao.model.db.VideoExample;
import com.jieshuibao.model.db.VideoExample.Criteria;
import com.jieshuibao.model.domain.VideoReq;
import com.jieshuibao.service.TvboxService;
import com.jieshuibao.service.VideoService;
import com.jieshuibao.util.StringUtils;

@Controller
@RequestMapping("tv")
public class TvVideoController {
	@Resource
	private VideoService videoService;
	@Resource
	private TvboxService tvboxService;
	
	@RequestMapping("video/list")
	@ResponseBody
	public Object list(HttpServletRequest request,@RequestBody VideoReq req) {
		PageRes res = new PageRes();
		String userid = request.getHeader("cookieId");
		if(StringUtils.isEmpty(userid)){
			res.setCode("0");
			res.setDes("系统繁忙!");
			return res;
		}
		Tvbox tvboxByDeviceNo = tvboxService.getTvboxByDeviceNo(userid);
		if(null == tvboxByDeviceNo){
			res.setCode("0");
			res.setDes("没有权限观看!");
			return res;
		}
		String type = req.getType();
		if(StringUtils.isEmpty(type)){
			res.setCode("0");
			res.setDes("系统繁忙!");
			return res;
		}
		int sinceId = req.getSinceId();
		int maxId = req.getMaxId();
		if (sinceId <= 0) {
			sinceId = 1;
		}
		if (maxId <= 0 || maxId < sinceId) {
			maxId = sinceId;
		}
		VideoExample videoExample = new VideoExample();
		Criteria criteria = videoExample.createCriteria();
		criteria.andTypeEqualTo(type);
		videoExample.setPageIndex(sinceId-1);
		videoExample.setPageSize(maxId-sinceId+1);
		videoExample.setOrderByClause("sort ");
		List<Video> videos = videoService.getPageVideo(videoExample );
		res.setCode("1");
		res.setData(videos);
		res.setTotalCount(videos.size());
		return res;
	}
}
